package com.hkd.common.core.domain;

//import com.fasterxml.jackson.annotation.JsonInclude;
import com.hkd.common.core.enums.ResultCode;
import lombok.*;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class R<T> {

    private int code;

    private String msg;

    private T data;


    public static <T> R<T> success() {
        return assembleResult(null, ResultCode.SUCCESS);
    }

    public static <T> R<T> success(T data) {
        return assembleResult(data, ResultCode.SUCCESS);
    }
    public static <T> R<T> success(T data, String msg) {
        return assembleResult(data, ResultCode.SUCCESS, msg);
    }

    public static <T> R<T> failed() {
        return assembleResult(null, ResultCode.FAILED);
    }

    public static <T> R<T> failed(int code, String msg) {
        return assembleResult(code, msg, null);
    }

    /**
     * 指定错误码
     *
     * @param resultCode 指定错误码
     * @param <T>
     * @return
     */
    public static <T> R<T> failed(ResultCode resultCode) {
        return assembleResult(null, resultCode);
    }

    private static <T> R<T> assembleResult(T data, ResultCode resultCode) {

        return new R (resultCode.getCode(), resultCode.getMessage(), data);
    }
    private static <T> R<T> assembleResult(T data, ResultCode resultCode, String msg) {

        return new R (resultCode.getCode(), msg, data);
    }

    private static <T> R<T> assembleResult(int code, String msg, T data) {
        return new R (code, msg, data);
    }
}
